package gameAppLayer.stateMachine.strategyState;

import view.EViewList;

import com.jme3.input.FlyByCamera;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;

import eventManager.AppEvent;
import eventManager.EventManager;
import eventManager.EventPortal;
import gameAppLayer.StrategyCamera;

/*
 * Messages eventManager to alter LOD of viewable objects depending on camera position
 */
public class GlobalLODControl extends AbstractControl {
	
//	private EventManager eventManager;
	private Node rootNode;
	//private IRealCamera camera;
	private boolean crossedThreshold;
	private float threshold = 3.0f;
	
	public GlobalLODControl(Node rootNode) {
//		this.eventManager = eventManager;
		this.rootNode = rootNode;
//		this.spatial = target;
//		spatial.addControl(this);
		//this.camera = camera;
	}
	
	public void setThreshold(float threshold) {
		this.threshold = threshold;
	}
	
//	public void setCamera(IRealCamera camera) {
//		this.camera = camera;
//	}
	
	public Control cloneForSpatial(Spatial spatial) {
		GlobalLODControl other = new GlobalLODControl(rootNode);
		other.setSpatial(spatial);
		return other;
	}

	@Override
	protected void controlUpdate(float tpf) {
		Vector3f location = spatial.getWorldTranslation();//camera.getLocation();
		
		//Level of detail changes
		if ((location.z > threshold) && !crossedThreshold) {
			Object[] objs = new Object[]{"Far"};
			EventPortal.queueEvent(new AppEvent("ChangeView", objs));
			crossedThreshold = true;
		}
		else if ((location.z < threshold) && crossedThreshold) {
			Object[] objs = new Object[]{"Near"};
			EventPortal.queueEvent(new AppEvent("ChangeView", objs));
			crossedThreshold = false;
		}
		
	}

	@Override
	protected void controlRender(RenderManager rm, ViewPort vp) {
		// TODO Auto-generated method stub
		
	}

}
